<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChFriendCircleTaskGroup;
use Hyperf\Database\Model\Collection;
use Mine\Abstracts\AbstractMapper;

class ChFriendCircleTaskGroupMapper extends AbstractMapper
{
    public function assignModel()
    {
        $this->model = ChFriendCircleTaskGroup::class;
    }

    /**
     * 获取分组列表
     * @param array $data
     * @return array|Collection|\Hyperf\Collection\Collection
     */
    public function getGroupList(array $data): array|Collection|\Hyperf\Collection\Collection
    {
        return $this->model::query()
            ->select(['id','name'])
            ->where('main_uid',$data['main_uid'])
            ->orderBy('id')
            ->get();
    }

    /**
     * 检查名称是否存在
     * @param string $name
     * @param mixed $id
     * @return bool
     */
    public function checkNameExists(string $name,mixed $id = null): bool
    {
        return $this->model::query()
            ->where('name',$name)
            ->when(!empty($id),function ($query) use ($id){
                $query->where('id','<>',$id);
            })->exists();
    }

    /**
     * 删除分组
     * @param mixed $id
     * @return bool|int|mixed|null
     * @throws \Exception
     */
    public function deleteById(mixed $id): mixed
    {
        return $this->model::query()->where('id',$id)->delete();
    }
}